package com.fzx.aidldemo;

import android.app.Application;
import android.app.Service;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.util.Log;

import com.ylz.aidlmanager.IAidlManager;


/**
 * 描述：
 * 项目名称：Ylz1302DriveDemo
 * 创建人：  FangZhixin
 * 创建时间：2018/3/7
 */

public class ServiceManager {

    private static final String TAG = "ServiceManager";
    private Context context;

    public interface OnServiceConnectedListener {

        void onServiceConnected(ComponentName name, IBinder service);

        void onServiceDisconnected(ComponentName name);

    }

    private volatile static ServiceManager serviceManager;

    private ServiceManager(Context context) {
        if (context instanceof Application) {
            this.context = context;
        } else {
            throw new RuntimeException("请传入ApplicationContext~~~~");
        }
    }

    public static ServiceManager getInstance(Context context) {
        if (serviceManager == null) {
            synchronized (ServiceManager.class) {
                if (serviceManager == null) {
                    serviceManager = new ServiceManager(context);
                }
            }
        }
        return serviceManager;
    }

    private ServiceConnection conn;
    private IAidlManager iAidlManager;
    private boolean isServiceBind;//是否绑定了aidl服务

    public void bindService(OnServiceConnectedListener listener) {
        bindService("com.ylz.aidlmanager", "com.ylz.aidlmanager.AidlService", listener);
    }

    public void bindService(String pkg, String action, final OnServiceConnectedListener listener) {
        Intent intent = new Intent();
        intent.setPackage(pkg);
        intent.setAction(action);
        conn = new ServiceConnection() {
            @Override
            public void onServiceConnected(ComponentName name, IBinder service) {
                if (service != null) {
                    Log.i(TAG, "onServiceConnected: 服务绑定成功");
                    if (listener != null) {
                        listener.onServiceConnected(name, service);
                    }
                    iAidlManager = IAidlManager.Stub.asInterface(service);
                    isServiceBind = true;
                }
            }

            @Override
            public void onServiceDisconnected(ComponentName name) {
                Log.i(TAG, "onServiceConnected: 服务绑定失败");
                if (listener != null) {
                    listener.onServiceDisconnected(name);
                }
                iAidlManager = null;
                isServiceBind = false;
            }
        };
        if (context != null) {
            isServiceBind = context.bindService(intent, conn, Service.BIND_AUTO_CREATE);
        } else {
            throw new RuntimeException("传入的Context为空");
        }

    }

    public IAidlManager getAidlManager() {
        return iAidlManager;
    }

    public void unBindService() {
        if (conn != null && isServiceBind) {
            context.unbindService(conn);
            isServiceBind = false;
        }
    }

    public boolean isServiceBind() {
        return isServiceBind;
    }

}
